package com.cee.admin.modules.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cee.admin.common.BaseController;
import com.cee.common.core.query.BasePageQuery;
import com.cee.common.web.result.Result;
import com.cee.domain.enums.FrontUserTypeEnum;
import com.cee.domain.modules.ent.vo.EntEmissionSourceOriginVo;
import com.cee.domain.modules.ent.vo.EntEmissionSourceVo;
import com.cee.domain.modules.sys.entity.SysFrontUser;
import com.cee.domain.modules.sys.form.FrontUserAuditForm;
import com.cee.domain.modules.sys.form.SysFrontUserForm;
import com.cee.domain.modules.sys.vo.SysFrontUserGetVo;
import com.cee.domain.modules.sys.vo.SysFrontUserSelectionBox;
import com.cee.domain.modules.sys.vo.SysFrontUserVo;
import com.cee.ds.common.constants.PermissionConstants;
import com.cee.ds.common.utils.BasePrincipalUtils;
import com.cee.ds.modules.ent.service.IEntEmissionSourceOriginService;
import com.cee.ds.modules.ent.service.IEntEmissionSourceService;
import com.cee.ds.modules.sys.service.ISysFrontUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wsz
 * @since 2024/1/15
 */
@Tag(name = "前台用户")
@RestController
@RequestMapping("/api/sys/frontUser")
public class SysFrontUserController extends BaseController {

    @Autowired
    private ISysFrontUserService frontUserService;

    @Autowired
    private IEntEmissionSourceOriginService entEmissionSourceOriginService;

    @Autowired
    private IEntEmissionSourceService entEmissionSourceService;

    @GetMapping("/page")
    @Operation(summary = "分页")
    @RequiresPermissions(PermissionConstants.SYS_FRONT_USER_PREFIX + PermissionConstants.READ)
    public Result<IPage<SysFrontUserVo>> page(BasePageQuery<SysFrontUserVo> basePageQuery, Integer userType, String loginName, String name, String auditStatus) {
        SysFrontUser queryEntity = new SysFrontUser();
        queryEntity.setLoginName(loginName);
        queryEntity.setName(name);
        queryEntity.setAuditStatus(auditStatus);
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        Integer areaId = BasePrincipalUtils.currentUser().getAreaId();
        Long manageParkId = BasePrincipalUtils.currentUser().getManageParkId();
        queryEntity.setCityId(cityId);
        queryEntity.setAreaId(areaId);
        queryEntity.setIndustrialParksId(manageParkId);
        queryEntity.setUserType(userType == null ? null : FrontUserTypeEnum.fromCode(userType));
        return success(frontUserService.queryPageVo(basePageQuery.generatePage(), queryEntity));
    }

    @PostMapping("/add")
    @Operation(summary = "新增", description = "返回新增后的用户id")
    @RequiresPermissions(PermissionConstants.SYS_FRONT_USER_PREFIX + PermissionConstants.ADD)
    public Result<Long> add(@RequestBody @Valid SysFrontUserForm addForm) {
        return success(frontUserService.add(addForm));
    }

    @PostMapping("/update")
    @Operation(summary = "更新", description = "修改用户")
    @RequiresPermissions(PermissionConstants.SYS_FRONT_USER_PREFIX + PermissionConstants.UPDATE)
    public Result<Boolean> update(@RequestBody @Valid SysFrontUserForm addForm) {
        return success(frontUserService.update(addForm));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除")
    @RequiresPermissions(PermissionConstants.SYS_FRONT_USER_PREFIX + PermissionConstants.DEL)
    public Result<Boolean> delete(@PathVariable Long id) {
        return success(frontUserService.deleteById(id));
    }

    @PostMapping("/audit")
    @Operation(summary = "审核")
    @RequiresPermissions(PermissionConstants.SYS_FRONT_USER_PREFIX + PermissionConstants.AUDIT)
    public Result<Boolean> audit(@Valid @RequestBody FrontUserAuditForm auditForm) {
        frontUserService.audit(auditForm);
        return success(true);
    }

    @GetMapping("/selectionBox")
    @Operation(summary = "选择框数据")
    public Result<List<SysFrontUserSelectionBox>> selectionBox(String loginName, String name, Integer industryId) {
        return success(frontUserService.selectionBox(loginName, name, industryId));
    }

    @GetMapping("emissionSource/list")
    @Operation(summary = "获取排放源")
    public Result<List<EntEmissionSourceVo>> emissionSourceList(@RequestParam @Parameter(description = "前台用户ID") Long userId) {
        return success(entEmissionSourceService.listVo(userId));
    }

    @GetMapping("emissionSource/fuelList")
    @Operation(summary = "获取燃料排放源集合")
    public Result<List<EntEmissionSourceVo>> fuelList(@RequestParam @Parameter(description = "前台用户ID") Long userId) {
        return success(entEmissionSourceService.listFuelVo(userId));
    }

    @GetMapping("emissionSourceOrigin/list")
    @Operation(summary = "获取排放源源流")
    public Result<List<EntEmissionSourceOriginVo>> emissionSourceOriginList(@RequestParam @Parameter(description = "前台用户ID") Long userId) {
        return success(entEmissionSourceOriginService.listVo(userId));
    }

    @GetMapping("exist")
    @Operation(summary = "账号是否存在", description = "存在则返回账号的ID")
    public Result<Long> exist(@RequestParam  @Parameter(description = "登录账号") String loginName) {
        SysFrontUser frontUser = frontUserService.getByLoginName(loginName);
        return success(frontUser == null ? null : frontUser.getId());
    }

    @GetMapping("get/{id}")
    @Operation(summary = "获取用户信息")
    @RequiresPermissions(PermissionConstants.SYS_FRONT_USER_PREFIX + PermissionConstants.READ)
    public Result<SysFrontUserGetVo> get(@PathVariable("id") Long id) {
        return success(frontUserService.getVo(id));
    }
}
